package com.hsd.ha.app.action;


import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.hsd.app.model.HsdDssinfo;
import com.hsd.commons.model.HsdAccount;
import com.hsd.core.base.action.BaseAction;
import com.hsd.core.cache.DictCache;
import com.hsd.core.page.entity.PageParams;
import com.hsd.core.utils.TimeUtil;
import com.hsd.ha.app.model.HaSurveyInst;
import com.hsd.ha.app.service.IHealthAssessmentService;
import com.hsd.ha.app.vo.FdtsVo;
import com.hsd.ha.cache.FdtsCache;



@Controller
@RequestMapping("/ha/fdts")
public class FdtsHaAction extends BaseAction{
	@Autowired
	private IHealthAssessmentService healthAssessmentService;
	 
	public void setHealthAssessmentService(
			IHealthAssessmentService healthAssessmentService) {
		this.healthAssessmentService = healthAssessmentService;
	}
	/**
	 * 健康测评 ：五维二步测评首页
	 * @param pageParams
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/loadFdtsIndex.do")
	public String loadFdtsIndex(PageParams pageParams,HttpServletRequest request,ModelMap model){
//		List datalist = FdtsCache.getInstance().getDataMapByAnswerid("8");
//		if(null != datalist){
//			for(int i =0;i<datalist.size();i++){
//				  System.out.println("data:" + datalist.get(i));
//			}
//		}
		return "app/evaluation/fdts/fdts-index";
	} 
	/**
	 * 健康测评 ：五维二步测评内容页
	 * @param pageParams
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/loadFdtsContent.do")
	public String loadFdtsContent(PageParams pageParams,HttpServletRequest request,ModelMap model){ 
		return "app/evaluation/fdts/fdts-content";
	} 
	/**
	 * 健康测评 ：五维二步测评结果
	 * @param pageParams
	 * @param request
	 * @param model
	 * @return
	 * @throws IOException 
	 */
 	@RequestMapping(value="/loadFdtsEvaResult.do")
	public String loadFdtsEvaResult(HttpServletRequest request,HttpServletResponse response,ModelMap model) throws IOException{ 
 		String valuestr = request.getParameter("answerstr");
 		System.out.println("VALUESTR:" + valuestr);
 		
 	//	valuestr = "{'1_0':{'groupNo':'1','paperName':'test','databaseNo':'0','questionNo':'0','questionType':'radio','answerArray':['1'],'answerIdArray':['1_test_0_0_1_0']},'1_1':{'groupNo':'1','paperName':'test','databaseNo':'68','questionNo':'1','questionType':'radio','answerArray':['3'],'answerIdArray':['1_test_68_1_3_0']},'1_2':{'groupNo':'1','paperName':'test','databaseNo':'69','questionNo':'2','questionType':'radio','answerArray':['4'],'answerIdArray':['1_test_69_2_4_0']},'1_3':{'groupNo':'1','paperName':'test','databaseNo':'70','questionNo':'3','questionType':'radio','answerArray':['11'],'answerIdArray':['1_test_70_3_11_0']},'1_4':{'groupNo':'1','paperName':'test','databaseNo':'71','questionNo':'4','questionType':'checkbox','answerArray':['13'],'answerIdArray':['1_test_71_4_13_0']},'2_1':{'groupNo':'2','paperName':'test','databaseNo':'72','questionNo':'1','questionType':'radio','answerArray':['17'],'answerIdArray':['2_test_72_1_17_0']},'2_2':{'groupNo':'2','paperName':'test','databaseNo':'73','questionNo':'2','questionType':'radio','answerArray':['25'],'answerIdArray':['2_test_73_2_25_0']},'2_3':{'groupNo':'2','paperName':'test','databaseNo':'74','questionNo':'3','questionType':'radio','answerArray':['29'],'answerIdArray':['2_test_74_3_29_0']},'2_4':{'groupNo':'2','paperName':'test','databaseNo':'75','questionNo':'4','questionType':'checkbox','answerArray':['35'],'answerIdArray':['2_test_75_4_35_0']},'3_1':{'groupNo':'3','paperName':'test','databaseNo':'76','questionNo':'1','questionType':'radio','answerArray':['39'],'answerIdArray':['3_test_76_1_39_0']},'3_2':{'groupNo':'3','paperName':'test','databaseNo':'77','questionNo':'2','questionType':'radio','answerArray':['42'],'answerIdArray':['3_test_77_2_42_0']},'3_3':{'groupNo':'3','paperName':'test','databaseNo':'78','questionNo':'3','questionType':'radio','answerArray':['52'],'answerIdArray':['3_test_78_3_52_0']},'3_4':{'groupNo':'3','paperName':'test','databaseNo':'79','questionNo':'4','questionType':'checkbox','answerArray':['54'],'answerIdArray':['3_test_79_4_54_0']},'3_5':{'groupNo':'3','paperName':'test','databaseNo':'80','questionNo':'5','questionType':'checkbox','answerArray':['59'],'answerIdArray':['3_test_80_5_59_0']},'4_1':{'groupNo':'4','paperName':'test','databaseNo':'81','questionNo':'1','questionType':'radio','answerArray':['62'],'answerIdArray':['4_test_81_1_62_0']},'4_2':{'groupNo':'4','paperName':'test','databaseNo':'82','questionNo':'2','questionType':'radio','answerArray':['66'],'answerIdArray':['4_test_82_2_66_0']},'4_3':{'groupNo':'4','paperName':'test','databaseNo':'83','questionNo':'3','questionType':'radio','answerArray':['72'],'answerIdArray':['4_test_83_3_72_0']},'4_4':{'groupNo':'4','paperName':'test','databaseNo':'84','questionNo':'4','questionType':'checkbox','answerArray':['77'],'answerIdArray':['4_test_84_4_77_0']},'5_1':{'groupNo':'5','paperName':'test','databaseNo':'85','questionNo':'1','questionType':'radio','answerArray':['83'],'answerIdArray':['5_test_85_1_83_0']},'5_2':{'groupNo':'5','paperName':'test','databaseNo':'86','questionNo':'2','questionType':'radio','answerArray':['87'],'answerIdArray':['5_test_86_2_87_0']},'5_3':{'groupNo':'5','paperName':'test','databaseNo':'87','questionNo':'3','questionType':'radio','answerArray':['90'],'answerIdArray':['5_test_87_3_90_0']},'5_4':{'groupNo':'5','paperName':'test','databaseNo':'88','questionNo':'4','questionType':'checkbox','answerArray':['93'],'answerIdArray':['5_test_88_4_93_0']},'5_5':{'groupNo':'5','paperName':'test','databaseNo':'89','questionNo':'5','questionType':'radio','answerArray':['100'],'answerIdArray':['5_test_89_5_100_0']},'5_6':{'groupNo':'5','paperName':'test','databaseNo':'90','questionNo':'6','questionType':'checkbox','answerArray':['103'],'answerIdArray':['5_test_90_6_103_0']},'6_1':{'groupNo':'6','paperName':'test','databaseNo':'91','questionNo':'1','questionType':'radio','answerArray':['109'],'answerIdArray':['6_test_91_1_109_0']},'6_2':{'groupNo':'6','paperName':'test','databaseNo':'92','questionNo':'2','questionType':'radio','answerArray':['114'],'answerIdArray':['6_test_92_2_114_0']},'6_3':{'groupNo':'6','paperName':'test','databaseNo':'93','questionNo':'3','questionType':'radio','answerArray':['119'],'answerIdArray':['6_test_93_3_119_0']},'6_4':{'groupNo':'6','paperName':'test','databaseNo':'94','questionNo':'4','questionType':'checkbox','answerArray':['125','130'],'answerIdArray':['6_test_94_4_125_0','6_test_94_4_130_0']},'7_1':{'groupNo':'7','paperName':'test','databaseNo':'95','questionNo':'1','questionType':'radio','answerArray':['142'],'answerIdArray':['7_test_95_1_142_0']},'7_2':{'groupNo':'7','paperName':'test','databaseNo':'96','questionNo':'2','questionType':'radio','answerArray':['146'],'answerIdArray':['7_test_96_2_146_0']},'7_3':{'groupNo':'7','paperName':'test','databaseNo':'97','questionNo':'3','questionType':'radio','answerArray':['149'],'answerIdArray':['7_test_97_3_149_0']},'7_4':{'groupNo':'7','paperName':'test','databaseNo':'98','questionNo':'4','questionType':'checkbox','answerArray':['155'],'answerIdArray':['7_test_98_4_155_0']}} ";
 		HsdAccount accinfo = this.getCurrentuser(request);
 		HaSurveyInst surveyInst = new HaSurveyInst();
 		Long accid = 0l; //游客
 		String username = "游客";
 		Long usersex = 0l;//未知
 		String userage = "-1";//未知
 		if(null != accinfo){
	 			accid = accinfo.getId();  
	 	 		username = accinfo.getUsername();
	 	 		usersex = accinfo.getUsersex(); 
	 	 		userage = accinfo.getUseage(); 
 		}
 		surveyInst.setAccid(accid);
 		surveyInst.setUsername(username);
 		surveyInst.setSurveydetail(valuestr);
 		surveyInst.setAgenum(userage);
 		surveyInst.setUsersex(usersex); 
 		surveyInst.setSurveystatus(1l);
 		surveyInst.setSurveyid(2l); 
 		surveyInst.setSurveydate(TimeUtil.getCurrentDateTime());
 		this.healthAssessmentService.save(surveyInst); 
 		System.out.println("=====valuestr=================:" + valuestr);
 		String  resultstr = this.healthAssessmentService.calculateFdtsResult(valuestr);
 		
 		
// 		//"yangxuzhi:50|yinxuzhi:50|qixuzhi:50|tanshizhi:50|shirezhi:50|xuetanzhi:50|tebingzhi:50|qiyuzhi:50|pinghezhi:50"
 		/*System.out.println("valuestr:" + valuestr);
 		System.out.println("main:" + phyObj.getMain_phy());
 		System.out.println("both:" + phyObj.getBoth_phy());
 		System.out.println("forward:" + phyObj.getForward_phy());
 		System.out.println("phyObj:" + phyObj.toString());
 		*/
// 		PrintWriter out = response.getWriter();
//	      out.print(resultstr);
//	      out.flush();
//	      out.close(); 
 		model.put("outstr", resultstr);
	     return "app/evaluation/fdts/fdts-result";
	} 
 	/**
	 * 健康测评 ：体质养生建议
	 * @param pageParams
	 * @param request
	 * @param model
	 * @return
	 * @throws IOException 
	 */
 	@RequestMapping(value="/loadFdtsDesc.do", method = RequestMethod.POST)
	public  void loadFdtsDesc(HttpServletRequest request,HttpServletResponse response,ModelMap model) throws IOException{
 		StringBuilder builderStr = new StringBuilder();
 		DictCache dictCache = DictCache.getInstance(); 
 		String zxid = request.getParameter("zxid");  
 		HsdDssinfo dssInfo = (HsdDssinfo)this.healthAssessmentService.findByID(HsdDssinfo.class, new Long(zxid)); 
		if(dssInfo != null){
 			String dssname = dssInfo.getDssname();
	 		builderStr.append("何为<font size=3><strong>").append(dssname).append("</strong></font><br>");
	 		builderStr.append(dssInfo.getDesccontent()).append("\r\n");
	 		 
	 		//获取该类体质人群的调养方案
	 		
	 		List hplist = this.healthAssessmentService.loadHpListbyId(dssInfo.getId());
	 		if(hplist.size() > 0 ){
	 			builderStr.append("<font size=3><strong>").append(dssname).append("</strong></font>的养生方案<br>");
		 		for(int i = 0;i<hplist.size();i++){
		 			Map data = (Map)hplist.get(i);
		 			String type = data.get("type")+"";
		 			String typevalue = dictCache.getCodeName("[调养方案类别]",type,null);
		 			String hpcontent = (String)data.get("hpcontent");
		 			builderStr.append(i+1).append(". ").append(typevalue).append("<br>");
		 			builderStr.append(hpcontent).append("<br>");
		 		}
	 		}
		}
		
	   response.setCharacterEncoding("UTF-8");
	   PrintWriter out = response.getWriter();
       out.print(builderStr.toString());
       out.flush();
       out.close(); 
	} 
	
}
